<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
    <el-form label-position="right" :model="facilityForm" :label-width="formLabelWidth" ref="refFacilityForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="公共设施名称" prop="name">
            <el-input v-model="facilityForm.name" placeholder="请输入公共设施名称" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="楼栋名称" prop="buildingName">
            <el-input v-model="facilityForm.buildingName" placeholder="请选择楼栋名称" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findBuilding"></i>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="位置" prop="location">
            <el-input v-model="facilityForm.location" placeholder="请输入位置" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="最后保养日期" prop="lastMaintenanceDate">
            <el-date-picker
                v-model="facilityForm.lastMaintenanceDate" type="date" placeholder="最后保养日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="状态" prop="status">
            <el-select v-model="facilityForm.status" placeholder="请选择状态">
              <el-option label="正常" :value="10"></el-option>
              <el-option label="异常" :value="20"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="buildingSelectorVisible" v-if="buildingSelectorVisible" title="楼栋信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <building-selector @building-selected-event="handleBuildingSelectedEvent">
      </building-selector>
    </el-dialog>
  </div>
</template>

<script>
import facilityApi from '@/api/facilityApi'
import BuildingSelector from "@/views/building/BuildingSelector.vue";
export default {
  name: 'FacilityAdd',
  components: {BuildingSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      facilityForm: {
        name: '',
        buildingId: '',
        buildingName: '',
        location: '',
        lastMaintenanceDate: '',
        status: ''
      },
      rules: {
      },
      buildingSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      facilityApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.facilityForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.facilityForm = {
        name: '',
        buildingId: '',
        buildingName: '',
        location: '',
        lastMaintenanceDate: '',
        status: ''
      }
      this.$emit('reset-facility-add-event')
    },
    onSave () {
      this.$refs.refFacilityForm.validate((valid) => {
        if (valid) {
          facilityApi.save(this.facilityForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-facility-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findBuilding () {
      this.buildingSelectorVisible = true
    },
    handleBuildingSelectedEvent (selectedBuilding) {
      this.buildingSelectorVisible = false
      if (selectedBuilding && 'buildingId' in selectedBuilding && this.facilityForm.buildingId !== selectedBuilding['buildingId']) {
        this.facilityForm.buildingId = selectedBuilding['buildingId']
        this.facilityForm.buildingName = selectedBuilding['buildingName']
      }
    },
    onBack () {
      this.$emit('close-facility-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>